//
//  WeChatLoginView.m
//  MeterMart
//
//  Created by ENERGY on 2018/10/9.
//  Copyright © 2018年 ENERGY. All rights reserved.
//

#import "WeChatLoginView.h"
//#import "CommonViewController.h"

@interface WeChatLoginView()
@property (nonatomic, strong)UIButton * checkBtn;//打钩按钮
@property (nonatomic, strong)UIButton * weChatCoverBtn;//微信覆盖点击事件按钮
@property (nonatomic, strong)UIButton * privacyBtn;//隐私
@property (nonatomic, strong)UIButton * policyBtn;//规则
@end

@implementation WeChatLoginView
- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        self.backgroundColor = [UIColor whiteColor];
        self.alpha = 0;
        //微信图标
        UIImageView * weChatImg = [[UIImageView alloc] init];
        weChatImg.image = [UIImage imageNamed:@"weixin"];
        [self addSubview:weChatImg];
        //微信标题
        UILabel * titleLab = [[UILabel alloc] init];
        titleLab.text = @"微信登录";
        titleLab.font = DEF_FontSize_14;
        titleLab.textAlignment = NSTextAlignmentCenter;
        titleLab.textColor = [UIColor blackColor];
        [self addSubview:titleLab];
        //添加点击事件
        UIButton * coverBtn = [[UIButton alloc] init];
        self.weChatCoverBtn = coverBtn;
        [coverBtn setBackgroundColor:[UIColor clearColor]];
        [coverBtn addTarget:self action:@selector(coverBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:coverBtn];
        //
        UILabel * agreeLab = [[UILabel alloc] init];
        agreeLab.text = @"我已同意";
        agreeLab.font = DEF_FontSize_14;
        [self addSubview:agreeLab];
        //打钩按钮
        self.checkBtn = [[UIButton alloc] init];
        [self.checkBtn setImage:[UIImage imageNamed:@"check"] forState:UIControlStateNormal];
        [self.checkBtn setImage:[UIImage imageNamed:@"uncheck"] forState:UIControlStateSelected];
        [self.checkBtn addTarget:self action:@selector(checkBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:self.checkBtn];
        //隐私规则
        self.privacyBtn = [[UIButton alloc] init];
        [self.privacyBtn setTitle:@"《隐私权政策》" forState:UIControlStateNormal];
        [self.privacyBtn setTitleColor:getColor(@"E6ACA6") forState:UIControlStateNormal];
        [self.privacyBtn addTarget:self action:@selector(privacyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        self.privacyBtn.titleLabel.font = DEF_FontSize_14;
        [self addSubview:self.privacyBtn];
        //和
        UILabel * andLab = [[UILabel alloc] init];
        andLab.text = @"和";
        andLab.font = DEF_FontSize_14;
        [self addSubview:andLab];
        //一米商城协议
        self.policyBtn = [[UIButton alloc] init];
        [self.policyBtn setTitle:@"《一米商团服务协议》" forState:UIControlStateNormal];
        [self.policyBtn setTitleColor:getColor(@"E6ACA6") forState:UIControlStateNormal];
        [self.policyBtn addTarget:self action:@selector(policyBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        self.policyBtn.titleLabel.font = DEF_FontSize_14;
        [self addSubview:self.policyBtn];
        //masonry
        __weak typeof (self)weakSelf = self;
        [weChatImg mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakSelf.mas_centerX);
            make.centerY.equalTo(weakSelf.mas_centerY);
            make.width.height.mas_offset(80/WIDTH_6_SCALE);
        }];
        [titleLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.right.equalTo(weakSelf);
            make.top.equalTo(weChatImg.mas_bottom).mas_offset(15/WIDTH_6_SCALE);
        }];
        [coverBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.top.right.equalTo(weChatImg);
            make.bottom.equalTo(titleLab.mas_bottom);
        }];
        [andLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.centerX.equalTo(weakSelf.mas_centerX).mas_offset(25/WIDTH_6_SCALE);
            make.bottom.equalTo(weakSelf.mas_bottom).mas_offset(-20/WIDTH_6_SCALE);
        }];
        [self.privacyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(andLab.mas_left);
            make.centerY.equalTo(andLab);
        }];
        [self.policyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(andLab.mas_right);
            make.centerY.equalTo(andLab);
        }];
        [self.checkBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(agreeLab.mas_left);
            make.centerY.equalTo(andLab);
            make.width.height.mas_offset(25/WIDTH_6_SCALE);
        }];
        [agreeLab mas_makeConstraints:^(MASConstraintMaker *make) {
            make.right.equalTo(weakSelf.privacyBtn.mas_left);
            make.centerY.equalTo(andLab);
        }];
    }return self;
}
#pragma mark - action
- (void)coverBtnClick:(UIButton *)sender{
    BOOL ifWeChatInstall = [WXApi isWXAppInstalled];
//    if (ifWeChatInstall) {
        if (self.weChatLongin) {
            self.weChatLongin();
        }
//    }else{
//        if (self.ifNotHaveWeChat) {
//            self.ifNotHaveWeChat();
//        }
//    }
}
- (void)weChatLonginViewDismiss{
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 0;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
//        [[NSNotificationCenter defaultCenter] removeObserver:self name:WXAuthorizationSuccess object:nil];
    }];
}
- (void)showWeChatLonginView{
    [[UIApplication sharedApplication].delegate.window addSubview:self];
    [UIView animateWithDuration:.15 animations:^{
        self.alpha = 1;
    }];
}
- (void)checkBtnClick:(UIButton *)sender{
    sender.selected = !sender.selected;
    if (sender.selected) {
        //不同意隐私规则和软件协议
        self.weChatCoverBtn.userInteractionEnabled = NO;
    }else{
        //同意
        self.weChatCoverBtn.userInteractionEnabled = YES;
    }
}
- (void)privacyBtnClick:(UIButton *)sender{
    if (self.presentControllerBlock) {
        self.presentControllerBlock(@"6");//隐私政策协议
    }
}
- (void)policyBtnClick:(UIButton *)sender{
    if (self.presentControllerBlock) {
        self.presentControllerBlock(@"5");//一米商团服务协议
    }
}

@end
